import { _BasicDataSource } from "../../_BasicDataSource"
import { uuid } from '../../util'

import { BaseEntity } from "../Entitys/BaseEntity";
import { Label } from "../Entitys/_Label";
import { Icon } from "../Entitys/_Icon";
import { Point } from '../Entitys/_Point'
import { LinkDataSource } from "./link";
import { ArrowDataSource } from "./arrow";
import { SatelliteDataSource } from "./satellite";
import { RadarDataSource } from "./radar";
import { TrailLine } from './TrailLine'

import { GetRotation, GetHeading, CreatePosition } from '../../Config.js'



var Layers = {
    arrow: ArrowDataSource,
    links: LinkDataSource,
    radar: RadarDataSource,
    satellite: SatelliteDataSource,
    TrailLine: TrailLine,
};



class PlatformEntity {
    constructor(options, parent) {
        var FS = window.REMFontSize || 1;
        this.options = options;
        this.id = options.id || uuid();
        this.parent = parent;
        this.EntityTypeName = 'PlatformEntity';
        // 基础平台类型
        this.BaseEntity = new BaseEntity({
            ...options,
            anime: parent.options.anime || false,
        });
        // 显示名称类型
        this.Label = new Label(this.BaseEntity.Entity, options.name, { OffsetX: 0, OffsetY: - options.pixelSize || (-40 * FS) }, options.pixelSize);

        this.Label.show = parent.options.defaultNameStatus;

        // 中心点（选择类型）
        this.Point = new Point(this.BaseEntity.Entity, options.color, options.pixelSize);
        // 图标类型
        this.Icon = new Icon(this.BaseEntity.Entity, options);
        // 切换图标显示类型
        this.Icon.changeSenceType(this.parent._SenceMode);

        // 输出用于图层加载的对象
        this.Entity = this.BaseEntity.Entity;
        this.Entity._Source = this;
        // 用于缓存link对象，用于触发刷新调用。
        this.links = [];

        this.TrailLine = null;

        if (this.options.TrailLine) {
            this.TrailLine = new TrailLine(this);
            this.parent.AddEntitys([this.TrailLine.MoveEntity, this.TrailLine.Entity]);
        };

    }
    setPosition(Position, time) {
        this.TrailLine && this.TrailLine.setPotions(CreatePosition(Position));
        this.BaseEntity.setPosition(Position, time, () => {
            this.Icon.changeRotation(GetRotation(Position, this.BaseEntity.Position));
        }, () => {
            for (let i = 0; i < this.links.length; i++) {
                const element = this.links[i];
                element.setPosition && element.setPosition(Position);
            }
        });
    }
    addLink(item) {
        if (!this.links.includes(item)) {
            this.links.push(item);
        }
    }
    removeLink(item) {
        this.links.forEach((_item, index) => {
            if (_item === item) {
                this.links.splice(index, 1);
                return false
            }
        });
    }
    destroy() {
        this.links = [];
        if (this.TrailLine) {
            this.parent.RemoveEntitys([this.TrailLine.MoveEntity, this.TrailLine.Entity]);
        }
    }
}


/**
 * 创建平台图层
 *
 * @class PlatformDataSource
 * @extends {_BasicDataSource}
 */
class PlatformDataSource extends _BasicDataSource {

    /**
     * 初始化
     * @param {simpleMap} parent 需要渲染的图层
     * @param {Object} options 图层需要的基本运行参数
     * @memberof PlatformDataSource
     */
    constructor(parent, options) {
        super(uuid(), parent, options);
        this.EntityTypeName = 'PlatformEntity'
        this.options = options || {};
        this.options.defaultNameStatus = !!options.defaultNameStatus;
    }

    /**
     * 是否显示图层
     *
     * @memberof _BasicDataSource
     */
    set show(value) {
        this._DataSource.show = !!value;
        for (let i = 0; i < this._Layers.length; i++) {
            const element = this._Layers[i];
            element.show = !!value;
        }

    }
    get show() {
        return this._DataSource.show
    }
    set LabelStatus(value) {
        this.options.defaultNameStatus = value;
        this.EachMapping(item => {
            item.Label.show = !!value;
        });
    }
    get LabelStatus() {
        return this.options.defaultNameStatus;
    }

    /**
     * 添加平台元素
     *
     * @param {*} options
     * @memberof PlatformDataSource
     */
    add(options) {
        const _Platform = new PlatformEntity(options, this);
        this._Mapping[_Platform.id] = _Platform;
        this._Entities.add(_Platform.Entity);
        return _Platform;
    }

    // showLabel(type) {
    //     this.EachMapping(item => {
    //         item.Label.show = !!type;
    //     });
    // }

    /**
     * 创建子图层
     *
     * @param {String} type 类型
     * @param {Object} options 子图层参数配置
     * @memberof PlatformDataSource
     */
    createLayer(type, options) {
        var _Layer = new Layers[type](this.parent, options, this);
        if (_Layer) {
            this.AddLayer(_Layer);
            return _Layer;
        } else {
            throw new Error('未找到对应图层信息');
        }
    }
}

export {
    PlatformEntity,
    PlatformDataSource
}
